package com.youlu.campus.admin.activity.service.impl;

import com.youlu.campus.activity.service.UserActivityTaskRankingService;
import com.youlu.campus.admin.activity.service.ActivityRankingService;
import com.youlu.campus.entity.ActivityInfo;
import com.youlu.campus.entity.ActivityTaskInfo;
import com.youlu.campus.entity.VO.res.ActivityRankingPointsVO;
import com.youlu.campus.entity.enums.TaskBizType;
import com.youlu.campus.service.activity.ActivityService;
import com.youlu.campus.service.activity.ActivityTaskService;
import com.youlu.campus.service.activity.UserParticipateActivityInfoService;
import com.youlu.campus.service.invite.UserInviteRecordService;
import com.youlu.campus.service.ranking.UserActivityPointRankingProvinceService;
import com.youlu.campus.service.ranking.UserActivityPointRankingService;
import com.youlu.campus.service.ranking.entity.UserActivityPointProvinceRankingQueryVo;
import com.youlu.campus.service.ranking.entity.UserActivityPointRankingQueryVo;
import com.youlu.campus.service.ranking.entity.UserActivityTaskRankingQueryVo;
import lombok.extern.slf4j.Slf4j;
import ma.glasnost.orika.MapperFacade;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class ActivityRankingServiceImpl implements ActivityRankingService {

    @Autowired
    private ActivityService activityService;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    protected MapperFacade mapperFacade;

    @Autowired
    private UserParticipateActivityInfoService userParticipateActivityInfoService;

    @Autowired
    private UserInviteRecordService userInviteRecordService;

    @Autowired
    private ActivityTaskService activityTaskService;

    @Autowired
    private UserActivityTaskRankingService userActivityTaskRankingService;

    @Autowired
    private UserActivityPointRankingService userActivityPointRankingService;

    @Autowired
    private UserActivityPointRankingProvinceService userActivityPointProvinceRankingService;

    /**
     * 初始化用户排行榜
     */
    public void initUserActivityRanking(String activityId) {
        long startTime = System.currentTimeMillis();
        userActivityTaskRankingService.updateUser(activityId);
        userActivityPointProvinceRankingService.updateUser(activityId);
        log.info("initUserActivityRanking activityId:{} 耗时:{}", activityId, (System.currentTimeMillis() - startTime) % 1000);
    }


    @Override
    public Page<ActivityRankingPointsVO> findByPage(ActivityRankingPointsVO activityRankingPoints) {
        String activityId = activityRankingPoints.getActivityId();// 活动id
        String province = activityRankingPoints.getProvince();// 省
        String universityId = activityRankingPoints.getUniversityId();// 大学id
        String nickName = activityRankingPoints.getNickName();// 昵称
        String mobile = activityRankingPoints.getMobile();// 手机号
        String userName = activityRankingPoints.getName();// 姓名
        Boolean countryRank = activityRankingPoints.getIsCountryRank();// 是否全国
        Integer page = activityRankingPoints.getPage();
        Integer pageSize = activityRankingPoints.getPageSize();

        ActivityInfo activityInfo = activityService.findOne(activityId);
        ActivityTaskInfo activityTaskInfo = activityTaskService.find(activityId, TaskBizType.INVITE);
        //查询全国榜单
        Page<ActivityRankingPointsVO> pageResult;
        if (countryRank) {
            if ("before".equals(activityInfo.getType())) {
                UserActivityTaskRankingQueryVo queryVo = new UserActivityTaskRankingQueryVo();
                queryVo.setName(nickName);
                queryVo.setUserName(userName);
                queryVo.setMobile(mobile);
                queryVo.setActivityId(activityId);
                queryVo.setUniversityId(universityId);
                queryVo.setPage(page);
                queryVo.setPageSize(pageSize);
                pageResult = userActivityTaskRankingService.findByPage(queryVo);
            } else {
                UserActivityPointRankingQueryVo queryVo = new UserActivityPointRankingQueryVo();
                queryVo.setName(nickName);
                queryVo.setUserName(userName);
                queryVo.setMobile(mobile);
                queryVo.setActivityId(activityId);
                queryVo.setUniversityId(universityId);
                queryVo.setPage(page);
                queryVo.setPageSize(pageSize);
                pageResult = userActivityPointRankingService.findByPage(queryVo);
            }
        } else {
            UserActivityPointProvinceRankingQueryVo queryVo = new UserActivityPointProvinceRankingQueryVo();
            queryVo.setName(nickName);
            queryVo.setProvince(province);
            queryVo.setUserName(userName);
            queryVo.setMobile(mobile);
            queryVo.setActivityId(activityId);
            queryVo.setUniversityId(universityId);
            queryVo.setPage(page);
            queryVo.setPageSize(pageSize);
            pageResult = userActivityPointProvinceRankingService.findByPage(queryVo);
        }
        //补充 邀请数
        List<ActivityRankingPointsVO> content = pageResult.getContent();
        if (CollectionUtils.isNotEmpty(content)) {
            for (ActivityRankingPointsVO pointsVO : content) {
                Long inviteUserCount = userInviteRecordService.getInviteUserCount(pointsVO.getUserId(), activityId, activityTaskInfo.getId(), Boolean.TRUE);
                pointsVO.setInviteNum(inviteUserCount != null ? inviteUserCount.toString() : "0");//邀请数
            }
        }
        return pageResult;
    }
}
